/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.core.template;

import com.jfinal.kit.PathKit;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

import java.io.File;

/**
 * @author michael yang (fuhai999@gmail.com)
 * @Date: 2020/3/17
 */
public class TemplateFilesWatcher extends Thread implements FileAlterationListener {

    private static final TemplateFilesWatcher me = new TemplateFilesWatcher();

    public static TemplateFilesWatcher me() {
        return me;
    }

    private int watchingInterval = 1000;

    private TemplateFilesWatcher() {
        setName("JPressTemplateFilesWatcher");
        setDaemon(false);
        setPriority(Thread.MAX_PRIORITY);
    }


    @Override
    public void run() {
        try {
            doRun();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }


    protected void doRun() throws Exception {
        FileAlterationObserver observer = new FileAlterationObserver(PathKit.getWebRootPath() + "/templates");
        observer.addListener(this);

        FileAlterationMonitor fileMonitor = new FileAlterationMonitor(watchingInterval, observer);
        addShutdownHook(fileMonitor);
        fileMonitor.start();
    }


    protected void addShutdownHook(FileAlterationMonitor watcher) {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                watcher.stop();
            } catch (Throwable e) {
            }
        }));
    }


    @Override
    public void onStart(FileAlterationObserver observer) {

    }

    @Override
    public void onDirectoryCreate(File directory) {

    }

    @Override
    public void onDirectoryChange(File directory) {

    }

    @Override
    public void onDirectoryDelete(File directory) {

    }

    @Override
    public void onFileCreate(File file) {
        TemplateManager.me().clearCache();
    }


    @Override
    public void onFileChange(File file) {
        TemplateManager.me().clearCache();
    }


    @Override
    public void onFileDelete(File file) {
        TemplateManager.me().clearCache();
    }

    @Override
    public void onStop(FileAlterationObserver observer) {

    }


    public static void main(String[] args) {
        TemplateFilesWatcher.me.start();
    }

}
